﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeiXinPF.Common;
using WeiXinPF.BLL;

namespace WeiXinPF.Web.admin.matrix
{
    public partial class wechat_matrix_edit : Web.UI.ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString(); //操作类型

        BLL.wx_wechat_matrix pBll = new BLL.wx_wechat_matrix();
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = MXRequest.GetQueryString("action");

            int id = 0;

            if (!string.IsNullOrEmpty(_action) && _action == MXEnums.ActionEnum.Edit.ToString())
            {
                id = MyCommFun.RequestInt("id");
                this.action = MXEnums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out id))
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (pBll.GetList(" id =" + id.ToString()).Tables[0].Rows.Count==0)
                {
                    JscriptMsg("记录不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {

                if (action == MXEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(id);
                }
                else
                {
                    this.ddlCategory.ClearSelection();
                }

            }
        }


        #region 赋值操作=================================
        private void ShowInfo(int id)
        {
            hidid.Value = id.ToString();
            Model.wx_wechat_matrix matrix = pBll.DataRowToModel(pBll.GetList(" id =" + id.ToString()).Tables[0].Rows[0]);
            hidid.Value = matrix.id.ToString();

            txttitle.Text = matrix.title.ToString();
            imgimg_link.ImageUrl = matrix.img_link;
            txtimg_link.Text = matrix.img_link;
            
            imgmatrix_link.ImageUrl = matrix.matrix_link;
            txtmatrix_link.Text = matrix.matrix_link;
            this.ddlCategory.ClearSelection();
            this.ddlCategory.SelectedValue = matrix.catelog;
            txtRemark.Text = matrix.remark;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();
            int id = MyCommFun.Str2Int(hidid.Value);
            Model.wx_wechat_matrix model = new Model.wx_wechat_matrix();


            if (id != 0)
            {
                model = pBll.DataRowToModel(pBll.GetList(" id =" + id.ToString()).Tables[0].Rows[0]);
            }

            string strErr = "";

            if (this.txttitle.Text.Trim().Length == 0)
            {
                strErr += "标题不能为空！\\n";
            }

            if (this.txtimg_link.Text.Trim().Length == 0)
            {
                strErr += "图标不能为空！\\n";
            }

            if (this.txtmatrix_link.Text.Trim().Length == 0)
            {
                strErr += "二维码链接不能为空！\\n";
            }
            model.wid = weixin.id;
            model.title = this.txttitle.Text;
            model.matrix_link = txtmatrix_link.Text;
            model.img_link = txtimg_link.Text;
            model.catelog = this.ddlCategory.SelectedValue;
            model.remark = txtRemark.Text;
            if (this.ddlCategory.SelectedValue == "官微")
            {
                this.ddlCategory.ClearSelection();
            }
            model.isdelete = false;
            model.modifydate = DateTime.Now;

            if (id != 0)
            {
                //update
                pBll.Update(model);
                AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "修改微信链接信息，主键为" + id); //记录日志
                JscriptMsg("修改微信链接成功！", "wechat_matrix_list.aspx", "Success");
            }
            else
            {
                model.createdate = DateTime.Now;
                pBll.Add(model);
                AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加微信链接，主键为" + id); //记录日志
                JscriptMsg("添加3微信链接成功！", "wechat_matrix_list.aspx", "Success");
            }
        }
        #endregion
    }
}